package cn.jianyongjie.user.service.impl;

import cn.jianyongjie.basic.constant.BaseConstant;
import cn.jianyongjie.basic.dto.VerificationCode;
import cn.jianyongjie.basic.exception.CustomException;
import cn.jianyongjie.basic.utils.MD5Utils;
import cn.jianyongjie.basic.utils.StrUtils;
import cn.jianyongjie.user.domain.Logininfo;
import cn.jianyongjie.user.domain.User;
import cn.jianyongjie.user.mapper.LogininfoMapper;
import cn.jianyongjie.user.mapper.UserMapper;
import cn.jianyongjie.user.service.IUserService;
import cn.jianyongjie.basic.service.impl.BaseServiceImpl;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianyongjie
 * @since 2022-05-08
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /*
    * @Title: UserServiceImpl
    * @Description: 手机注册
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 16:11
    * @Parameters: * @param code
    * @Return void
    */
    @Override
    public void phoneRegister(VerificationCode code) {
        //抽出重复代码
        String phone = code.getPhone();
        String password = code.getPassword();
        String passwordRepeat = code.getPasswordRepeat();
        String verificationCode = code.getVerificationCode();
        if (phone==null){
            throw new CustomException("电话不能为空");
        }

        if (password==null){
            throw new CustomException("密码不能为空");
        }

        if (passwordRepeat==null){
            throw new CustomException("确认密码不能为空");
        }

        if (verificationCode==null){
            throw new CustomException("短信验证码不能为空");
        }

        // 查询手机号是否已经被注册
        User user = userMapper.LoadByPhone(phone);

        if (user!=null){
            throw new CustomException("该手机号已经被注册了，请登录");
        }


        // 判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new CustomException("两次密码不一致，请重新输入");
        }

        // 使用全局常量类拼接电话得到拼接后的参数
        String businessKey = BaseConstant.REGISTER_VERIFICATION_CODE+ phone;
        // 使用拼接后的参数查询redis里面的短信验证码
        Object smsCode = redisTemplate.opsForValue().get(businessKey);
        // 将时间戳去掉，进行比较
        String sms = smsCode.toString().split(":")[0];
        // 判断验证码是否一致
        if (smsCode==null){
            // 如果等于null，就代表短信验证码已经过期了
            throw new CustomException("短信验证码已经过期，请重新获取");
        }else {
            // 如果不等于null，就判断用户输入的验证码是否一致
            if (!verificationCode.equalsIgnoreCase(sms)){
                throw new CustomException("短信验证码不一致请重新输入");
            }
        }

        // 保存t_logininfo和t_user表
        User dtoUser = dtoUser(code);
        Logininfo info = dtoLogininfo(dtoUser);

        // 进行新增,但先新增t_logininfo
        logininfoMapper.save(info);
        // 将新增的t_logininfo的id设置给t_user表
        dtoUser.setLogininfoId(info.getId());
        // 进行新增用户
        userMapper.save(dtoUser);
    }

    private Logininfo dtoLogininfo(User dtoUser) {

        Logininfo logininfo = new Logininfo();
        // 拷贝user设置的参数
        BeanUtils.copyProperties(dtoUser,logininfo);
        // 设置type
        logininfo.setType(1);
        return logininfo;
    }

    private User dtoUser(VerificationCode code) {
        User user = new User();
        // 将名称设置为电话
        user.setUsername(code.getPhone());
        // 设置电话
        user.setPhone(code.getPhone());
        // 设置时间
        user.setCreatetime(new Date());
        // 设置邮件
        user.setEmail(code.getEmail());
        // 设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        //2 通过这个盐值加密密码
        String md5Pwd = MD5Utils.encrypByMd5(code.getPassword()+salt);
        // 保存盐值
        user.setSalt(salt);
        // 保存被盐值加密的密码
        user.setPassword(md5Pwd);
        return user;
    }


    /*
    * @Title: UserServiceImpl
    * @Description: 邮箱注册
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 20:04
    * @Parameters: * @param mailbox
    * @Return void
    */
    @Override
    public void mailboxRegister(VerificationCode mailbox) {

        // 抽出重复代码
        String password = mailbox.getPassword();
        String passwordRepeat = mailbox.getPasswordRepeat();
        String email = mailbox.getEmail();

        // 判断邮件是否已经注册了
        User user = userMapper.LoadByEmail(email);

        if (user!=null){
            throw new CustomException("邮件已经被注册了，请登录");
        }

        // 判断两次密码是否一致
        if (!password.equals(passwordRepeat)){
            throw new CustomException("两次密码不一致，请重新输入");
        }


        // 进行保存
        User dtoUser = dtoUser(mailbox);
        Logininfo info = dtoLogininfo(dtoUser);

        // 进行新增,但先新增t_logininfo
        logininfoMapper.save(info);
        // 将新增的t_logininfo的id设置给t_user表
        dtoUser.setLogininfoId(info.getId());
        // 进行新增用户
        userMapper.save(dtoUser);
    }
}
